package com.atguigu.srb.core.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.srb.core.mapper.DictMapper;
import com.atguigu.srb.core.pojo.dto.ExcelDictDTO;

import java.util.ArrayList;
import java.util.List;

public class ExcelDictListener extends AnalysisEventListener<ExcelDictDTO> {

    DictMapper dictMapper;

    List<ExcelDictDTO> excelDictDTOS = new ArrayList<>();

    private static Long count = 0L;

    public ExcelDictListener(DictMapper baseMapper) {
        this.dictMapper = baseMapper;
    }

    @Override
    public void invoke(ExcelDictDTO excelDictDTO, AnalysisContext analysisContext) {
        excelDictDTOS.add(excelDictDTO);
        if (excelDictDTOS.size()>= 10){
            System.out.println("每读取十行数据，插入"+ ++count +"次");
            dictMapper.insertDictBatch(excelDictDTOS);
            excelDictDTOS.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("断后，兄弟们先走，我断后");
        dictMapper.insertDictBatch(excelDictDTOS);
    }
}
